<html xmlns="http://www.w3.org/1999/xhtml"><head><style type="text/css">
dt {
  font-style: italic;
  margin-top: 15px;
  margin-bottom: 3px;
  margin-left: 0px;
  border-bottom: 1px dotted black;
}
dd {
  margin-left: 10px;
}
table {
  border-collapse:collapse;
  border: 1px solid black;
  margin-top: 7px;
}
th {
  border: 1px solid black;
  padding: 3 7 3 7;
}
td {
  border: 1px solid black;
  padding: 3 7 3 7;
}
</style></head><body><p><a name="multianewarray"></a><b>multianewarray</b> :
  allocate multi-dimensional array : <a href="opcodes.html">index</a> : <a href="http://asm.objectweb.org/asm40/javadoc/user/org/objectweb/asm/MethodVisitor.html#visitMultiANewArrayInsn(java.lang.String,%20int)">visitMultiANewArrayInsn()</a></p><dl><dt>Description</dt><dd>Allocates a multi-dimensional array. In Java, a multi-dimensional array
is structured an array of arrays, i.e. an array whose elements are references
to array objects. So constructing an array like:<br></br><pre>
new int [3][2]
</pre>
produces
a structure in memory like:<br></br>    
<A xmlns="" href="ref01.gif">Click here for Picture </A><br></br>   <br></br>In this particular example the top level array is an array containing three
32-bit references. Each of these references identifies a two-element int
array.<br></br>The same general approach applies to higher dimension arrays. For example, a
three-dimensional array is structured as a top level array of arrayrefs, each
of which refers to an array of arrayrefs, each of which refers to an array of
items.<br></br>The lengths of each array within the multi-dimensional array are given as
positive ints on the operand stack. The number of ints taken from the stack is
specified by the unsigned byte parameter &lt;n&gt;. The type of the array is
given as an array type descriptor by the &lt;type&gt; parameter.<br></br>The &lt;type&gt; is first resolved to a Java class (see Chapter 7 for a
description of how array type descriptors are resolved).
multianewarray then allocates the first &lt;n&gt; dimensions of a
multidimensional array from the heap. If &lt;n&gt; is one, only the top-level
array is created. So the statement:<br></br><pre>
new int [3][]
</pre>
generates
JVM code like:<br></br><pre>
bipush 3
multianewarray [[I 1    ; construct first dimension of the 2-D array
</pre>
in
this case only the three-element top-level array is created. <br></br>To construct both the top-level array and its sub-arrays, e.g. to fully
construct<br></br><pre>
new int [3][2]
</pre>
use
the JVM code:<br></br><pre>
bipush 3
bipush 2
multianewarray [[I 2    ; construct both dimensions of the 2-D array
</pre>
Here
the second parameter given to multianewarray is 2, so the first and
second dimensions are created (i.e. the top-level array and the array objects
within the top-level array). <br></br>&lt;n&gt; must be less than or equal to the dimensionality given in the array's
descriptor - i.e. it must be less than or equal to the number of '[' characters
at the start of &lt;type&gt;.<br></br>The elements within the array are initialized to zero (for numeric and boolean
arrays), or null for arrays of references. multianewarray
leaves a reference to the newly constructed array on the operand stack.</dd><dt>Exceptions</dt><dd>NegativeArraySizeException - size is less than zero<br></br>OutOfMemoryError - insufficient memory to allocate the array</dd><dt>Example</dt><dd><pre>
; to allocate an array like:
;     new String[2][5]
    bipush 2
    bipush 5
    multianewarray [[Ljava/lang/String; 2   ; construct both dimensions
    ; stack now hold a reference two the new two dimensional array.
;
; multianewarray can be used to allocate only some of the
; dimensions of an array. For example, you write:
;
;     x = new int[6][3][]
;
; using:
    bipush 6
    bipush 3
    multianewarray [[[I 2  ; allocate 2 of the 3 dimensions
    astore_1               ; store array in local variable 1
; then you can create the final dimensions later using
; newarray or anewarray. e.g.
;
;    x[0][1] = new int[50];
;
; would be:
    aload_1         ; push the array
    iconst_0
    aaload          ; get x[0] - an array of arrays of ints
    iconst_1
    bipush 50
    newarray int    ; allocate an array of 50 ints
    aastore         ; store this in x[0][1]
;
; You can also use multianewarray to create
; single-dimensional arrays. e.g.
    bipush 50
    multianewarray [Ljava/lang/Thread; 1
; is the same as writing:
    bipush 50
    anewarray java/lang/Thread
; except that the latter is more efficient.
</pre></dd><dt>Notes</dt><dd>You can use multianewarray to allocate single-dimension arrays,
though using anewarray or newarray is more efficient.</dd><dt>See also</dt><dd><a href="ref-newarray.html">newarray</a> <a href="ref-.html"></a> <a href="ref-anewarray.html">anewarray</a> <a href="ref-.html"></a> <a href="ref-new.html">new</a> </dd><dt>Stack</dt><dd><table xmlns="">
<tr>
<td>
<b>Before </b>
</td>
<td><b>After</b></td></tr>
<tr>
<td>sizeN</td>
<td>arrayref</td></tr>
<tr>
<td>---</td>
<td>...</td></tr>
<tr>
<td>size3</td>
<td>...</td></tr>
<tr>
<td>size2</td>
<td>...</td></tr>
<tr>
<td>size1</td>
<td>...</td></tr>
<tr>
<td>...</td>
<td>...</td></tr>
</table></dd><dt>Bytecode</dt><dd><table xmlns="">
<tr>
<td>
<b> Type </b>
</td>
<td><b>Description</b></td></tr>
<tr>
<td>
u1 
</td>
<td>multianewarray
opcode = 0xC5 (197)
</td></tr>
<tr>
<td>
u2 
</td>
<td>index</td></tr>
<tr>
<td>
u1 
</td>
<td>&lt;n&gt;</td></tr>
</table></dd></dl></body></html>